package com.frcdi.qms.material.service;

import com.frcdi.qms.material.domain.InboundRecord;
import com.frcdi.qms.material.domain.Materials;
import com.frcdi.qms.material.domain.OutboundRecord;
import com.frcdi.qms.material.mapper.InboundRecordMapper;
import com.frcdi.qms.material.mapper.MaterialsMapper;
import com.frcdi.qms.material.mapper.OutboundRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName OutboundService
 * @Author Mr.Han
 * @Description //TODO $
 * @create 2024/7/8 17:39
 **/
@Service
public class OutboundService {
    @Autowired
    private OutboundRecordMapper outboundRecordMapper;
    @Autowired
    private MaterialsMapper materialMapper;
    @Autowired
    private InboundRecordMapper inboundRecordMapper;

    public void addOutboundRecord(Integer materialId, List<OutboundRecord> records) {
        Materials material = materialMapper.selectById(materialId);
        List<InboundRecord> inboundRecords = inboundRecordMapper.selectByMaterialId(materialId);

        for (OutboundRecord record : records) {
            int remainingQuantity = record.getQuantity();
            for (InboundRecord inboundRecord : inboundRecords) {
                if (inboundRecord.getQuantity() <= remainingQuantity) {
                    remainingQuantity -= inboundRecord.getQuantity();
                    outboundRecordMapper.insert(record);
                } else {
                    break;
                }
            }
        }
    }

    public List<OutboundRecord> getOutboundRecordsByMaterialId(Integer materialId) {
        return outboundRecordMapper.selectByMaterialId(materialId);
    }
}

